library("rstudioapi")     
##Set working directory
setwd(dirname(getActiveDocumentContext()$path))
getwd()
library("readxl") 
require(miceadds)
library(data.table)
library(tidyverse)
library(DescTools)
library(htmlTable)
library(stargazer)
library(estimatr)
library(tidyverse)
library(DescTools)
require(dplyr)

load("FinalFinal20062023Full1-6.RData")
combo = final_finalV2

combo$Q25_combo <- ifelse(!is.na(combo$p_ii_Q25), combo$p_ii_Q25, combo$p_iii_Q25)
combo$Q26_combo <- ifelse(!is.na(combo$p_ii_Q26), combo$p_ii_Q26, combo$p_iii_Q26)
combo$Q27_combo <- ifelse(!is.na(combo$p_ii_Q27), combo$p_ii_Q27, combo$p_iii_Q27)
combo$Q2.2_combo <- ifelse(!is.na(combo$p_ii_Q2.2), combo$p_ii_Q2.2, combo$p_iii_Q2.2)
combo$Q2.3_combo <- ifelse(!is.na(combo$p_ii_Q2.3), combo$p_ii_Q2.3, combo$p_iii_Q2.3)

## r for subsetting: 
combo = combo[which(combo$p_iii == 1),]
dim(combo)
##1: 
# Number of villages:
nb_vil_full = length(na.omit(unique(combo$Q123)))

nb_vil <- combo %>%
  group_by(VillageTreatment, Q123) %>% summarise(n = n()) %>%
  summarise(n = n())

nb_vil_pla = nb_vil$n[nb_vil$VillageTreatment == "Placebo"][1]
nb_vil_cdc = nb_vil$n[nb_vil$VillageTreatment == "CDC health"][1]
nb_vil_lc = nb_vil$n[nb_vil$VillageTreatment == "Low Cash"][1]
nb_vil_hc = nb_vil$n[nb_vil$VillageTreatment == "High Cash"][1]

##2: 
# Number of observations:
nb_treatment <- combo %>% group_by(individual_treatment) %>% summarise(n = n())

nb_t1 <- nb_treatment$n[nb_treatment$individual_treatment == "Placebo"][1]
nb_t2 <- nb_treatment$n[nb_treatment$individual_treatment == "CDC Health"][1]
nb_t3 <- nb_treatment$n[nb_treatment$individual_treatment == "Low Cash"][1]
nb_t4 <- nb_treatment$n[nb_treatment$individual_treatment == "High Cash"][1]

nb_total <- sum(nb_treatment$n)

##3: 
# % Female
gender_full <- combo%>%  group_by(Q2.3) %>% summarise(n = n()) %>%
  mutate(percentage = 100 * n / sum(n))
female_full <- gender_full$percentage[gender_full$Q2.3 == "Female"][1]

# % Female per treatment
gender_treatment <- combo %>%  group_by(individual_treatment, Q2.3) %>%
  summarise(n = n()) %>%   mutate(percentage = 100 * n / sum(n))

female_pla <- gender_treatment$percentage[gender_treatment$Q2.3 == "Female" & gender_treatment$individual_treatment == "Placebo"][1]
female_cdc <- gender_treatment$percentage[gender_treatment$Q2.3 == "Female" & gender_treatment$individual_treatment == "CDC Health"][1]
female_lc <- gender_treatment$percentage[gender_treatment$Q2.3 == "Female" & gender_treatment$individual_treatment == "Low Cash"][1]
female_hc <- gender_treatment$percentage[gender_treatment$Q2.3 == "Female" & gender_treatment$individual_treatment == "High Cash"][1]

# SD for Female variable: 
f_tab = combo[,c("individual_treatment", "Q2.3")]
f_tab$Female = ifelse(f_tab$Q2.3=="Female", 1, 0)
female_sd <- f_tab %>%
  group_by(individual_treatment) %>%
  summarise(sd = sd(Female, na.rm = TRUE))
female_pla_sd <- as.numeric(female_sd[which(female_sd$individual_treatment=="Placebo"),][2])*100
female_cdc_sd <- as.numeric(female_sd[which(female_sd$individual_treatment=="CDC Health"),][2])*100
female_lc_sd <- as.numeric(female_sd[which(female_sd$individual_treatment=="Low Cash"),][2])*100
female_hc_sd <-as.numeric(female_sd[which(female_sd$individual_treatment=="High Cash"),][2])*100
female_total_sd = sd(f_tab$Female, na.rm = TRUE)*100

##4: 
# vaccinated: Y/N
combo_small = combo

vaccination_full = combo_small %>%  group_by(vaccine_reported_combo) %>% summarise(n = n()) %>%
  mutate(percentage = 100 * n / sum(n))
vaccined_full = vaccination_full$percentage[vaccination_full$vaccine_reported_combo == 1][1]

vaccine_treatment <- combo_small %>%  group_by(individual_treatment, vaccine_reported_combo) %>%
  summarise(n = n()) %>%   mutate(percentage = 100 * n / sum(n))
vaccine_pla <- vaccine_treatment$percentage[vaccine_treatment$vaccine_reported_combo == 1 & vaccine_treatment$individual_treatment == "Placebo"][1]
vaccine_cdc <- vaccine_treatment$percentage[vaccine_treatment$vaccine_reported_combo == 1 & vaccine_treatment$individual_treatment == "CDC Health"][1]
vaccine_lc <- vaccine_treatment$percentage[vaccine_treatment$vaccine_reported_combo == 1 & vaccine_treatment$individual_treatment == "Low Cash"][1]
vaccine_hc <- vaccine_treatment$percentage[vaccine_treatment$vaccine_reported_combo == 1 & vaccine_treatment$individual_treatment == "High Cash"][1]

# Standard deviations: 
v_tab = combo_small[,c("individual_treatment", "vaccine_reported_combo")]
v_tab$vaccine = ifelse(v_tab$vaccine_reported_combo=="1", 1, 0)
vaccine_sd <- v_tab %>%
  group_by(individual_treatment) %>%
  summarise(sd = sd(vaccine, na.rm = TRUE))
vaccine_pla_sd <- as.numeric(vaccine_sd[which(vaccine_sd$individual_treatment=="Placebo"),][2])*100
vaccine_cdc_sd <- as.numeric(vaccine_sd[which(vaccine_sd$individual_treatment=="CDC Health"),][2])*100
vaccine_lc_sd <- as.numeric(vaccine_sd[which(vaccine_sd$individual_treatment=="Low Cash"),][2])*100
vaccine_hc_sd <-as.numeric(vaccine_sd[which(vaccine_sd$individual_treatment=="High Cash"),][2])*100
vaccine_total_sd = sd(v_tab$vaccine, na.rm = TRUE)*100

##5: 
# How many villiges have you visited last month: 
vil_month_total = mean(as.numeric(combo$Q25_combo), na.rm = TRUE)
combo$Q25_combo = as.numeric(combo$Q25_combo)

vil_month_tr <- combo %>%
  group_by(individual_treatment) %>%
  summarise(mean = mean(Q25_combo,  na.rm = TRUE))

vil_month_tr1 = vil_month_tr$mean[vil_month_tr$individual_treatment == "Placebo"][1]
vil_month_tr2 = vil_month_tr$mean[vil_month_tr$individual_treatment == "CDC Health"][1]
vil_month_tr3 = vil_month_tr$mean[vil_month_tr$individual_treatment == "Low Cash"][1]
vil_month_tr4 = vil_month_tr$mean[vil_month_tr$individual_treatment == "High Cash"][1]

# standard deviations
vil_month_total_sd = sd(as.numeric(combo$Q25_combo), na.rm = TRUE)

vil_month_tr_sd <- combo %>%
  group_by(individual_treatment) %>%
  summarise(mean = sd(Q25_combo,  na.rm = TRUE))

vil_month_tr1_sd = vil_month_tr_sd$mean[vil_month_tr_sd$individual_treatment == "Placebo"][1]
vil_month_tr2_sd = vil_month_tr_sd$mean[vil_month_tr_sd$individual_treatment == "CDC Health"][1]
vil_month_tr3_sd = vil_month_tr_sd$mean[vil_month_tr_sd$individual_treatment == "Low Cash"][1]
vil_month_tr4_sd = vil_month_tr_sd$mean[vil_month_tr_sd$individual_treatment == "High Cash"][1]


##6: Q26_combo
# How many villiges have you visited last year: 
vil_year_total = mean(as.numeric(combo$Q26_combo), na.rm = TRUE)
combo$Q26_combo = as.numeric(combo$Q26_combo)

vil_year_tr <- combo %>%
  group_by(individual_treatment) %>%
  summarise(mean = mean(Q26_combo,  na.rm = TRUE))

vil_year_tr1 = vil_year_tr$mean[vil_year_tr$individual_treatment == "Placebo"][1]
vil_year_tr2 = vil_year_tr$mean[vil_year_tr$individual_treatment == "CDC Health"][1]
vil_year_tr3 = vil_year_tr$mean[vil_year_tr$individual_treatment == "Low Cash"][1]
vil_year_tr4 = vil_year_tr$mean[vil_year_tr$individual_treatment == "High Cash"][1]

# standard deviations
vil_year_total_sd = sd(as.numeric(combo$Q26_combo), na.rm = TRUE)

vil_year_tr_sd <- combo %>%
  group_by(individual_treatment) %>%
  summarise(mean = sd(Q26_combo,  na.rm = TRUE))

vil_year_tr1_sd = vil_year_tr_sd$mean[vil_year_tr_sd$individual_treatment == "Placebo"][1]
vil_year_tr2_sd = vil_year_tr_sd$mean[vil_year_tr_sd$individual_treatment == "CDC Health"][1]
vil_year_tr3_sd = vil_year_tr_sd$mean[vil_year_tr_sd$individual_treatment == "Low Cash"][1]
vil_year_tr4_sd = vil_year_tr_sd$mean[vil_year_tr_sd$individual_treatment == "High Cash"][1]

##7: Q27_combo
# Percent of people having family in other villages 
fami_vil_full = combo %>%  group_by(Q27_combo) %>% summarise(n = n()) %>%
  mutate(percentage = 100 * n / sum(n))
fami_vil_full = fami_vil_full$percentage[fami_vil_full$Q27_combo == "Yes"][1]

fami_vil_treatment <- combo %>%  group_by(individual_treatment, Q27_combo) %>%
  summarise(n = n()) %>%   mutate(percentage = 100 * n / sum(n))
fami_vil_pla <- fami_vil_treatment$percentage[fami_vil_treatment$Q27_combo == "Yes" & fami_vil_treatment$individual_treatment == "Placebo"][1]
fami_vil_cdc <- fami_vil_treatment$percentage[fami_vil_treatment$Q27_combo == "Yes" & fami_vil_treatment$individual_treatment == "CDC Health"][1]
fami_vil_lc <- fami_vil_treatment$percentage[fami_vil_treatment$Q27_combo == "Yes" & fami_vil_treatment$individual_treatment == "Low Cash"][1]
fami_vil_hc <- fami_vil_treatment$percentage[fami_vil_treatment$Q27_combo == "Yes" & fami_vil_treatment$individual_treatment == "High Cash"][1]

# Standard deviations: 
fam_tab = combo[,c("individual_treatment", "Q27_combo")]
fam_tab$family = ifelse(fam_tab$Q27_combo=="Yes", 1, 0)
fam_sd <- fam_tab %>%
  group_by(individual_treatment) %>%
  summarise(sd = sd(family, na.rm = TRUE))
fam_pla_sd <- as.numeric(fam_sd[which(fam_sd$individual_treatment=="Placebo"),][2])*100
fam_cdc_sd <- as.numeric(fam_sd[which(fam_sd$individual_treatment=="CDC Health"),][2])*100
fam_lc_sd <- as.numeric(fam_sd[which(fam_sd$individual_treatment=="Low Cash"),][2])*100
fam_hc_sd <-as.numeric(fam_sd[which(fam_sd$individual_treatment=="High Cash"),][2])*100
fam_total_sd = sd(fam_tab$family, na.rm = TRUE)*100

##8: Q2.2_combo
# Percentage of having whatsapp: 
whatsapp_full = combo %>%  group_by(Q2.2_combo) %>% summarise(n = n()) %>%
  mutate(percentage = 100 * n / sum(n))
whatsapp_full = whatsapp_full$percentage[whatsapp_full$Q2.2_combo == "Yes"][1]

whatsapp_treatment <- combo %>%  group_by(individual_treatment, Q2.2_combo) %>%
  summarise(n = n()) %>%   mutate(percentage = 100 * n / sum(n))
whatsapp_pla <- whatsapp_treatment$percentage[whatsapp_treatment$Q2.2_combo == "Yes" & whatsapp_treatment$individual_treatment == "Placebo"][1]
whatsapp_cdc <- whatsapp_treatment$percentage[whatsapp_treatment$Q2.2_combo == "Yes" & whatsapp_treatment$individual_treatment == "CDC Health"][1]
whatsapp_lc <- whatsapp_treatment$percentage[whatsapp_treatment$Q2.2_combo == "Yes" & whatsapp_treatment$individual_treatment == "Low Cash"][1]
whatsapp_hc <- whatsapp_treatment$percentage[whatsapp_treatment$Q2.2_combo == "Yes" & whatsapp_treatment$individual_treatment == "High Cash"][1]

# Standard deviations: 
whatsapp_tab = combo[,c("individual_treatment", "Q2.2_combo")]
whatsapp_tab$whatsapp = ifelse(whatsapp_tab$Q2.2_combo=="Yes", 1, 0)
whatsapp_sd <- whatsapp_tab %>%
  group_by(individual_treatment) %>%
  summarise(sd = sd(whatsapp, na.rm = TRUE))
whatsapp_pla_sd <- as.numeric(whatsapp_sd[which(whatsapp_sd$individual_treatment=="Placebo"),][2])*100
whatsapp_cdc_sd <- as.numeric(whatsapp_sd[which(whatsapp_sd$individual_treatment=="CDC Health"),][2])*100
whatsapp_lc_sd <- as.numeric(whatsapp_sd[which(whatsapp_sd$individual_treatment=="Low Cash"),][2])*100
whatsapp_hc_sd <-as.numeric(whatsapp_sd[which(whatsapp_sd$individual_treatment=="High Cash"),][2])*100
whatsapp_total_sd = sd(whatsapp_tab$whatsapp, na.rm = TRUE)*100

##9: Q2.3_combo
# how often do you use Whatsapp - % at least once per month
combo_small = combo
combo_small$Q2.3_combo[which(combo_small$Q2.3_combo=="Never")] = NA
combo_small = combo_small[-which(is.na(combo_small$Q2.3_combo)==TRUE),]
#dim(combo_small)
whatsapp_use_full = combo_small %>%  group_by(Q2.3_combo) %>% summarise(n = n()) %>%
  mutate(percentage = 100 * n / sum(n))
whatsapp_use_full = whatsapp_use_full$percentage[whatsapp_use_full$Q2.3_combo == "A few times each week"][1] +
  whatsapp_use_full$percentage[whatsapp_use_full$Q2.3_combo == "A few times every day"][1] +
  whatsapp_use_full$percentage[whatsapp_use_full$Q2.3_combo == "About once a day"][1] +
  whatsapp_use_full$percentage[whatsapp_use_full$Q2.3_combo == "About once a week"][1] +
  whatsapp_use_full$percentage[whatsapp_use_full$Q2.3_combo == "Many times, every day"][1] +
  whatsapp_use_full$percentage[whatsapp_use_full$Q2.3_combo == "Once or twice a month"][1]
whatsapp_use_treatment = combo_small %>%  group_by(individual_treatment, Q2.3_combo) %>%
  summarise(n = n()) %>%   mutate(percentage = 100 * n / sum(n))

whatsapp_use_tr1 = sum(whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "A few times each week" & whatsapp_use_treatment$individual_treatment == "Placebo"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "A few times every day" & whatsapp_use_treatment$individual_treatment == "Placebo"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "About once a day"& whatsapp_use_treatment$individual_treatment == "Placebo"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "About once a week"& whatsapp_use_treatment$individual_treatment == "Placebo"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "Many times, every day"& whatsapp_use_treatment$individual_treatment == "Placebo"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "Once or twice a month"& whatsapp_use_treatment$individual_treatment == "Placebo"][1], na.rm =TRUE)

whatsapp_use_tr2 = sum(whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "A few times each week" & whatsapp_use_treatment$individual_treatment == "CDC Health"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "A few times every day" & whatsapp_use_treatment$individual_treatment == "CDC Health"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "About once a day"& whatsapp_use_treatment$individual_treatment == "CDC Health"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "About once a week"& whatsapp_use_treatment$individual_treatment == "CDC Health"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "Many times, every day"& whatsapp_use_treatment$individual_treatment == "CDC Health"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "Once or twice a month"& whatsapp_use_treatment$individual_treatment == "CDC Health"][1], na.rm =TRUE)

whatsapp_use_tr3 = sum(whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "A few times each week" & whatsapp_use_treatment$individual_treatment == "Low Cash" ][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "A few times every day" & whatsapp_use_treatment$individual_treatment == "Low Cash"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "About once a day"& whatsapp_use_treatment$individual_treatment == "Low Cash"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "About once a week"& whatsapp_use_treatment$individual_treatment == "Low Cash"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "Many times, every day"& whatsapp_use_treatment$individual_treatment == "Low Cash"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "Once or twice a month"& whatsapp_use_treatment$individual_treatment == "Low Cash"][1], na.rm = TRUE)

whatsapp_use_tr4 = sum(whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "A few times each week" & whatsapp_use_treatment$individual_treatment == "High Cash" ][1], 
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "A few times every day" & whatsapp_use_treatment$individual_treatment == "High Cash"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "About once a day"& whatsapp_use_treatment$individual_treatment == "High Cash"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "About once a week"& whatsapp_use_treatment$individual_treatment == "High Cash"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "Many times, every day"& whatsapp_use_treatment$individual_treatment == "High Cash"][1],
                       whatsapp_use_treatment$percentage[whatsapp_use_treatment$Q2.3_combo == "Once or twice a month"& whatsapp_use_treatment$individual_treatment == "High Cash"][1], na.rm = TRUE)

w_cmb = combo_small[,c("individual_treatment","Q2.3_combo")]
w_cmb$use = ifelse(w_cmb$Q2.3_combo %in% c("A few times each week",
                                           "A few times every day", 
                                           "About once a day",
                                           "About once a week", "Many times, every day",
                                           "Once or twice a month") , 1, 0)
w_cmb_sd <- w_cmb %>%
  group_by(individual_treatment) %>%
  summarise(sd = sd(use, na.rm = TRUE)) 

w_pla_sd = as.numeric(w_cmb_sd[which(w_cmb_sd$individual_treatment=="Placebo"),][2])*100
w_cdc_sd = as.numeric(w_cmb_sd[which(w_cmb_sd$individual_treatment=="CDC Health"),][2])*100
w_lc_sd = as.numeric(w_cmb_sd[which(w_cmb_sd$individual_treatment=="Low Cash"),][2])*100
w_hc_sd = as.numeric(w_cmb_sd[which(w_cmb_sd$individual_treatment=="High Cash"),][2])*100
w_total_sd = sd(w_cmb$use, na.rm = TRUE)*100


#### Build a new final Table: 
e = c(0)
#Vectors for the table
Total_Sample <- round(c(nb_vil_full, nb_total, female_full, vaccined_full,
                        vil_month_total, vil_year_total, fami_vil_full, whatsapp_full, 
                        whatsapp_use_full), digits=1)

Total_Sample_sd = round(c(e, e, female_total_sd,  vaccine_total_sd,
                          vil_month_total_sd, vil_year_total_sd, fam_total_sd, whatsapp_total_sd,
                          w_total_sd), digits=1)

Placebo <- round(c(nb_vil_pla, nb_t1, female_pla, vaccine_pla,
                   vil_month_tr1, vil_year_tr1, fami_vil_pla, whatsapp_pla,
                   whatsapp_use_tr1), digits=1)

Placebo_sd <- round(c(e,e, female_pla_sd, vaccine_pla_sd, 
                      vil_month_tr1_sd, vil_year_tr1_sd, fam_pla_sd, whatsapp_pla_sd,
                      w_pla_sd), digits=1)

Health_Message <- round(c(nb_vil_cdc, nb_t2, female_cdc, vaccine_cdc,
                          vil_month_tr2, vil_year_tr2, fami_vil_cdc, whatsapp_cdc,
                          whatsapp_use_tr2), digits=1)

Health_Message_sd <- round(c(e, e, female_cdc_sd, vaccine_cdc_sd,
                             vil_month_tr2_sd, vil_year_tr2_sd, fam_cdc_sd, whatsapp_cdc_sd,
                             w_cdc_sd), digits=1)

Low_Cost <- round(c(nb_vil_lc, nb_t3, female_lc, vaccine_lc, 
                    vil_month_tr3, vil_year_tr3, fami_vil_lc, whatsapp_lc,
                    whatsapp_use_tr3), digits=1)

Low_Cost_sd <- round(c(e, e, female_lc_sd, vaccine_lc_sd,
                       vil_month_tr3_sd, vil_year_tr3_sd, fam_lc_sd, whatsapp_lc_sd,
                       w_lc_sd), digits=1)

High_Cost <- round(c(nb_vil_hc, nb_t4, female_hc, vaccine_hc,
                     vil_month_tr4, vil_year_tr4, fami_vil_hc, whatsapp_hc,
                     whatsapp_use_tr4), digits=1)

High_Cost_sd <- round(c(e, e, female_hc_sd, vaccine_hc_sd,
                        vil_month_tr4_sd, vil_year_tr4_sd, fam_hc_sd, whatsapp_hc_sd,
                        w_hc_sd), digits=1)

col1 = c()
for (i in c(1:9)){
  if (i %in% c(1,2)){
    col1[i] = Total_Sample[i]
  } else {
    col1[i] = as.character(paste(Total_Sample[i],paste("(",Total_Sample_sd[i],")", sep ="")))
  }
  
}
col1

col2 = c()
for (i in c(1:9)){
  if (i %in% c(1,2)){
    col2[i] = Placebo[i]
  } else {
    col2[i] = as.character(paste(Placebo[i],paste("(",Placebo_sd[i],")", sep ="")))
  }
  
}
col2

col3 = c()
for (i in c(1:9)){
  if (i %in% c(1,2)){
    col3[i] = Health_Message[i]
  } else {
    col3[i] = as.character(paste(Health_Message[i],paste("(",Health_Message_sd[i],")", sep ="")))
  }
  
}
col3

col4 = c()
for (i in c(1:9)){
  if (i %in% c(1,2)){
    col4[i] = Low_Cost[i]
  } else {
    col4[i] = as.character(paste(Low_Cost[i],paste("(",Low_Cost_sd[i],")", sep ="")))
  }
  
}
col4

col5 = c()
for (i in c(1:9)){
  if (i %in% c(1,2)){
    col5[i] = High_Cost[i]
  } else {
    col5[i] = as.character(paste(High_Cost[i],paste("(",High_Cost_sd[i],")", sep ="")))
  }
  
}
col5

test = cbind(col1, col2, col3, col4, col5)
library(htmlTable)
htmlTable(test)


# new row names: 
img_col_names <- c("Number of Villages", "Number of Subjects", 
                   "% Female", "% Reported Vaccinated", 
                   "Mean villages visited last month",
                   "Mean villages visited last year",
                   "% with family in other villages",
                   "% with WhatsApp", 
                   "% WhatsApp >= once per month")



df_fig_tt <- data.frame(img_col_names, test)
colnames(df_fig_tt) <- c("", "Total Sample", "Placebo", "Health Message", "Low Cost", "High Cost")
htmlTable(df_fig_tt[-9,])
htmlTable(df_fig_tt)
require(xtable)
print.xtable(df_fig_tt, file = "ExtDat_Table1_PanelB.tex", compress = FALSE)
print(xtable(df_fig_tt, type = "latex"), include.rownames = FALSE)



